package com.wuwei.tran.controller;

import com.wuwei.tran.domain.MyFile;
import com.wuwei.tran.service.FileService;
import com.wuwei.tran.utils.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping(value="tran/folder")
@CrossOrigin
public class FolderController {

    @Value("${ROOT_PATH}")
    private String filePath;

    /**
     * 新建文件夹
     * @return
     */
    @PostMapping("newFolder")
    public Result<String> newFolder(@RequestBody MyFile file){
        String name = file.getName();
        Path path = Paths.get(filePath +"\\"+ file.getPath()+ name);
        if (Files.exists(path)) {
            return Result.fail("创建失败！,该目录已经存在");
        }
        try {
            Files.createDirectories(path);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("创建失败！");
        }
        return Result.ok("创建成功！");
    }

    @Resource
    private FileService fileService;

    @GetMapping("enterFolder")
    public Result<List<MyFile>> enterFolder (@RequestParam("path") String path){
        List<MyFile> list = fileService.getList( "",path);
        return Result.ok(list);
    }
}
